package com.example.examratingbarlist;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class RateListDemo extends Activity  {
	
	TextView selection;
	ListView myList;
	
	String[] item = {"this","is","a","really","really1",
			"really2", "really3","really4","really5","really6","silly","list",
			"silly1","thoai","story","baby","lovely"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		myList = (ListView) findViewById(R.id.list);
		ArrayList<RowModel> list = new ArrayList<RowModel>();
		
		for(String s: item){
			list.add(new RowModel(s));
		}
		//su dung listActivity
		//setListAdapter(new CheckAdapter(this, list));
		myList.setAdapter(new CheckAdapter(this, list));
		//su dung Activity bt
		selection = (TextView) findViewById(R.id.selection);
		
		myList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> av, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				selection.setText(getModel(position).toString());
				Log.d("thoasadf", "thoadf");
			}
			
		
			
		});
	}	
	
	private RowModel getModel(int position){
		//su dung listActivity
		//return (((CheckAdapter)getListAdapter()).getItem(position));	
		
		return (((CheckAdapter)myList.getAdapter()).getItem(position));		
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id){		
		String s = getModel(position).toString();
		selection.setText(s);
		
	}	
	
	public class CheckAdapter extends ArrayAdapter<RowModel>{
		Activity context;
		
		CheckAdapter(Activity context,ArrayList<RowModel> list){
			
			super(context,R.layout.myrow,list);
			this.context = context;
		}
		public View getView(int position, View convertView, ViewGroup parent){
			View row = convertView;
			ViewWrapper wrapper ;
			RatingBar rate;
			
			if(row == null){
				LayoutInflater inflater = context.getLayoutInflater();
				
				row = inflater.inflate(R.layout.myrow, null);
				wrapper = new ViewWrapper(row);
				row.setTag(wrapper);
				rate = wrapper.getRatingBar();
				
				
				RatingBar.OnRatingBarChangeListener l = 
						new RatingBar.OnRatingBarChangeListener() {
							
					//khi nao thay doi rate thi moi thuc hien phuong thuc nay
							@Override
							public void onRatingChanged(RatingBar ratingBar, float rating,
									boolean fromTouch) {
								// TODO Auto-generated method stub
								Integer myPosition = (Integer) ratingBar.getTag();
								RowModel model = getModel(myPosition);
								
								model.rating = rating;
								
								LinearLayout parent = (LinearLayout) ratingBar.getParent();
								TextView label = (TextView) parent.findViewById(R.id.label);
								
								label.setText(model.toString());
							}
						};						
					rate.setOnRatingBarChangeListener(l);
					
			}
			else{
				wrapper = (ViewWrapper) row.getTag();
				rate = wrapper.getRatingBar();
			}
			
			RowModel model = getModel(position);
			
			wrapper.getLabel().setText(model.toString());
			rate.setTag(new Integer(position));
			rate.setRating(model.rating);
			
			return row;
		}
		
	}
	class RowModel{
		
		String label;
		float rating = 2.0f;
		
		RowModel(String label){
			this.label = label;
		}
		
		public String toString(){
			if(rating >= 3)
				return(label.toUpperCase());
			
			return label;
		}
	}
	
	class ViewWrapper{
		View base;
		RatingBar rate = null;
		TextView label = null;
		
		ViewWrapper (View base){
			this.base = base;
		}
		
		RatingBar getRatingBar(){
			if(rate == null){
				rate = (RatingBar) base.findViewById(R.id.rate);
			}
			return rate;
		}
		
		TextView getLabel(){
			if(label == null){
				label = (TextView) base.findViewById(R.id.label);
			}
			return label;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.rate_list_demo, menu);
		return true;
	}

}
